/**
 * @file
 * @auth jinguangguo
 * @date 2017/01/19


    当前委托 及  历史委托 组件

 */

import Vue from 'vue';
import filter from '../../../../grsc-base/util/filter';
import Pager from '../../../../grsc-base/component/Pager.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import Loading from '../../../../grsc-base/component/Loading.vue';
import {PAGE_SIZE} from '../../../../grsc-base/common/constants.js';
import MathTool from '../../../../grsc-base/util/mathtool';
import lib from '../../../common/lib';
import tpl from './tpl.html';
import service from './service';
import util from './util';
import CONFIG from './config';
import commonService from '../../../common/commonService';


export default Vue.extend({
    name: 'MyOrder',
    template: tpl,
    components: {
        Pager,
        Loading,
        NoData
    },
    props: {

    },
    data() {
        let coin = this.$$symbol.split('_')[1];


        return {
            CONFIG: CONFIG,
            list: [],
            isLoading: true,
            pageNo: 1,
            pageNum: 0,
            coin: coin,
            timer: null
        };
    },
    created() {
        let that = this;
        commonService.onLoopGetMyCurOrder({
            symbol: that.$$symbol,
            pageNo: that.pageNo,
            pageSize: PAGE_SIZE
        },(rep) => {
            that.isLoading = false;
            if (rep.code == window.CODE.SUCCESS) {
                that.list = that._getFormattedList(rep.data.list);
                that.pageNum = rep.data.pageNum;
                // 如果当前页内容为空, 返回上一页
                if (that.list.length === 0) {
                    if(that.pageNo>1){
                        that.pageNo--;
                        that.fetchList();
                    }
                }
            } else if(rep.code == window.CODE.NO_LOGIN.CODE){

            } else{
                window.error(rep.msg);
            }
        });



        // this.timer = setInterval(()=>{
        //     this.fetchList();
        // },1000);
        
        // $(document).off('$$assets.OrderList').on('$$assets.OrderList', () => {
        //     this.fetchList();
        // });
    },
    beforeDestroy(){
        clearInterval(this.timer);
    },
    methods: {
        /**
         * 获取数据列表
         */
        // fetchList() {
        //     let pageSize = PAGE_SIZE;
        //     service.tradeCoinCurrentList({
        //         symbol: this.$$symbol,
        //         pageNo: this.pageNo,
        //         pageSize: pageSize
        //     }).done((rep) => {
        //         this.isLoading = false;
        //         if (rep.code == window.CODE.SUCCESS) {
        //             this.list = this._getFormattedList(rep.data.list);
        //             this.pageNum = rep.data.pageNum;
        //             // 如果当前页内容为空, 返回上一页
        //             if (this.list.length === 0) {
        //                 if(this.pageNo>1){
        //                     this.pageNo--;
        //                     this.fetchList();
        //                 }
        //             }
        //         } else if(rep.code == window.CODE.NO_LOGIN.CODE){

        //         } else{
        //             window.error(rep.msg);
        //         }
        //     }).fail((rep) => {

        //     })
        // },

        /**
         * 改变分页  暂时废弃
         */
        // changePage(pageNo) {
        //     this.pageNo = pageNo;
        //     this.fetchList()
        // },

        /**
         * 撤单
         * @param item
         * @param $event
         */
        cancelOrder(item, $event) {
            let that = this;
            window.confirm({
                target: $event.target,
                content: that.$t('myOrder.areYouSure'),
                confirmBtnText:that.$t('common.yes'),
                cancelBtnText:that.$t('common.no'),
            }).done(() => {
                service.tradeCoinCancelByid({
                    symbol: this.$$symbol,
                    orderNo: item.payTransactionNo
                }).done(rep => {
                    if (rep.code === window.CODE.SUCCESS) {
                        // this.fetchList();

                        // 更新资产数据
                        $(document).trigger('$$assets');
                    } else {
                        if (rep.code === window.CODE.ORDER_BIND_HAS_SUCCESS.CODE) {
                            window.error(window.CODE.ORDER_BIND_HAS_SUCCESS[this.$i18n.locale], () => {
                                // this.fetchList();
                            });
                        } else if (rep.code === window.CODE.ORDER_CANCEL.CODE) {
                            window.error(window.CODE.ORDER_CANCEL[this.$i18n.locale], () => {
                                // this.fetchList();
                            });
                        } else {
                            window.error(rep.msg);
                        }
                    }
                });
            }).fail(() => {

            });
        },

        /**
         * 格式化列表数据
         * @param list
         * @returns {*}
         * @private
         */
        _getFormattedList(list) {
            if (list.length === 0) {
                return [];
            }

            let that = this;
            $.map(list, (item, index) => {
                // 日期
                // item.createDateHtml = util.getDateHtml(item.createDate);
                // 类型
                item.tradeCoinTypeHtml = util.getTradeTypeHtml(item.tradeCoinType);

                // 市价
                if (item.tradeCoinFlag === CONFIG.TRADE_FLAG_MARKET.KEY) {
                    item.priceText = 'Market Price';
                    if (item.tradeCoinType == CONFIG.TRADE_TYPE_SELL.KEY) {
                        // 委托数量
                        item.numberHtml = `${filter.floorFix(item.number,that.$$amountPrecision)} ${that.coin}`;
                        // 成交数量 - 果仁
                        // item.tradedCoinHtml = `<i class="icon icon-g"></i>${filter.floorFix(item.tradedCoin)}`;
                        // 剩余数量 - 果仁
                        item.numberOverHtml = `${filter.floorFix(item.numberOver,that.$$amountPrecision)} ${that.coin}`;
                    } else {
                        // 委托数量
                        item.numberHtml = `${filter.floorFix(item.money, this.$$coinPrecision) +' '+ that.$$symbol.split('_')[0]} `;
                        // 成交数量 - currencyout
                        // item.tradedCoinHtml = `￥${filter.floorFix(item.totalTraded)}`;
                        // 剩余数量 - currencyout
                        item.numberOverHtml = `${filter.floorFix(item.moneyOver)  +' '+ that.$$symbol.split('_')[0] }`;
                    }
                } else {    // 限价
                    item.priceText = filter.floorFixPrice(item.price, this.$$coinPrecision) + ' ' + that.$$symbol.split('_')[0];
                    // 委托数量
                    item.numberHtml = `${filter.floorFix(item.number,that.$$amountPrecision)} ${that.coin}`;
                    // 成交数量
                    item.tradedCoinHtml = `${filter.floorFix(MathTool.reduce(item.number, item.numberOver),that.$$amountPrecision)} ${that.coin}`;
                    // 剩余数量
                    item.numberOverHtml = `${filter.floorFix(item.numberOver,that.$$amountPrecision)} ${that.coin}`;
                }
            });
            return list;
        }
    }
});